package cn.belong.jk.controller;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.util.CellRangeAddress;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import cn.belong.jk.entity.OutProductVO;
import cn.belong.jk.service.OutProductService;
import cn.belong.jk.util.DownloadUtil;

@Controller
@RequestMapping("outproduct")
public class OutProductController extends BaseController {

    @Resource  
    OutProductService outProductService; 
    //转向输入年月的页面  
    @RequestMapping("/toedit.action")  
    public String toedit(){  
        return "/cargo/outproduct/jOutProduct.jsp";  
    }  
      
    //打印  
    @RequestMapping("/print.action")  
    public void print(String inputDate, HttpServletRequest request, HttpServletResponse response) throws IOException{  //inputDate格式:yyyy-MM  
        
        List<OutProductVO> dataList=outProductService.find(inputDate);  
        /*System.out.println(dataList.size()); 
        System.out.println(inputDate);*/  
      
      
        Workbook wb=new XSSFWorkbook();  
        Sheet sheet=wb.createSheet();  
        Row nRow=null;  
        Cell nCell=null;  
          
        int rowNo=0;  //行号  
        int cellNo=1;//列号  
          
        //声明样式对象和字体对象  
        CellStyle nStyle=wb.createCellStyle();  
        Font nFont=wb.createFont();  
          
        //设置表格宽度  
        sheet.setColumnWidth(0,1*300);//列宽  
        sheet.setColumnWidth(1,26*300);//列宽  
        sheet.setColumnWidth(2,12*300);//列宽  
        sheet.setColumnWidth(3,29*300);//列宽  
        sheet.setColumnWidth(4,10*300);//列宽  
        sheet.setColumnWidth(5,12*300);//列宽  
        sheet.setColumnWidth(6,8*300);//列宽  
        sheet.setColumnWidth(7,10*300);//列宽  
        sheet.setColumnWidth(8,10*300);//列宽  
        sheet.setColumnWidth(9,8*300);//列宽  
          
        //大标题，合并单元格  
        sheet.addMergedRegion(new CellRangeAddress(0,0,1,9));//开会行，结束行，开始列，结束列  
        //合并单元格的内容是写在合并前第一个单元格中  
        nRow=sheet.createRow(rowNo++);  
        nRow.setHeightInPoints(36);//行高  
          
        nCell=nRow.createCell(1);  
        nCell.setCellValue(inputDate.replaceFirst("-0","-").replaceFirst("-","年")+"月份出货表");//yyyy-MM  
        nCell.setCellStyle(this.bigTitle(wb, nStyle, nFont));  
          
        //配置标题行  
        String [] title=new String[]{"客户","订单号","货号","数量","工厂","附件","工厂交期","船期","贸易条款"};  
          
        nRow=sheet.createRow(rowNo++);  
        nRow.setHeightInPoints(26.25f);//标题的高  
        nStyle=wb.createCellStyle();//重新初始化样式  
        nFont=wb.createFont();//重新初始化字体  
        for (int i = 0; i < title.length; i++) {  
            nCell=nRow.createCell(i+1);  
            nCell.setCellValue(title[i]);  
            nCell.setCellStyle(this.Title(wb, nStyle, nFont));  
        }  
          
        nStyle=wb.createCellStyle();//重新初始化样式  
        nFont=wb.createFont();//重新初始化字体  
          
        //处理数据  
        for (int i = 0; i < dataList.size(); i++) {  
            OutProductVO op=dataList.get(i);  
      
      
            nRow=sheet.createRow(rowNo++);  
            nRow.setHeightInPoints(24);//数据框的高  
            cellNo=1;//列号初始化  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getCustomName());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getContractNo());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
               
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getProductNo());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getCnumber());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getFactoryName());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getExts());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getDelivery_preriod());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getShip_time());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
            nCell=nRow.createCell(cellNo++);  
            nCell.setCellValue(op.getTradeTerms());  
            nCell.setCellStyle(this.Text(wb, nStyle, nFont));  
              
        }  
          
      //下载(使用了我们的工具类)  
        DownloadUtil du=new DownloadUtil();  
        ByteArrayOutputStream bs=new ByteArrayOutputStream();  
        wb.write(bs);  
        du.download(bs, response, "出货表.xlsx");  
    }  
    
    
    
  //大标题样式  
    public CellStyle bigTitle(Workbook wb,CellStyle nStyle,Font nFont){  
        nFont.setFontName("宋体");  
        nFont.setFontHeightInPoints((short)16);  
        //设置字体加粗  
        nFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
        //横向居中  
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);  
        //纵向居中  
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);  
        nStyle.setFont(nFont);  
          
        return nStyle;  
    }  
    
  //标题样式  
    public CellStyle Title(Workbook wb,CellStyle nStyle,Font nFont){  
        nFont.setFontName("黑体");  
        nFont.setFontHeightInPoints((short)12);  
        //设置字体加粗  
        nFont.setBoldweight(HSSFFont.BOLDWEIGHT_BOLD);  
        //横向居中  
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);  
        //纵向居中  
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);  
        //表格线(上下左右的细线)  
        nStyle.setBorderTop(CellStyle.BORDER_THIN);  
        nStyle.setBorderLeft(CellStyle.BORDER_THIN);  
        nStyle.setBorderRight(CellStyle.BORDER_THIN);  
        nStyle.setBorderBottom(CellStyle.BORDER_THIN);  
          
        nStyle.setFont(nFont);  
          
        return nStyle;  
    }  
      
      
    //文字样式  
    public CellStyle Text(Workbook wb,CellStyle nStyle,Font nFont){  
        nFont.setFontName("Times New Roman");  
        nFont.setFontHeightInPoints((short)10);  
        //横向居中  
        nStyle.setAlignment(CellStyle.ALIGN_CENTER);  
        //纵向居中  
        nStyle.setVerticalAlignment(CellStyle.VERTICAL_CENTER);  
        //表格线(上下左右的细线)  
        nStyle.setBorderTop(CellStyle.BORDER_THIN);  
        nStyle.setBorderLeft(CellStyle.BORDER_THIN);  
        nStyle.setBorderRight(CellStyle.BORDER_THIN);  
        nStyle.setBorderBottom(CellStyle.BORDER_THIN);  
          
        nStyle.setFont(nFont);  
          
        return nStyle;  
    }  
}
